package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.CouponMeal;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.CouponMealService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.RequestContext;

import java.util.List;

/**
 * 优惠券套餐Controller
 *
 * @author jing
 * @date 2021-05-25
 */
@RestController
@RequestMapping("/meal")
public class CouponMealController extends BaseController {
    @Autowired
    private CouponMealService couponMealService;

    /**
     * 查询优惠券套餐列表
     */
    @GetMapping("/list")
    public JsonResult list(CouponMeal couponMeal) {
        startPage();
        List<CouponMeal> list = couponMealService.selectCouponMealList(couponMeal);
        Integer count = couponMealService.selectCouponMealCount(couponMeal);
        return JsonResult.ok(list, count);
    }

    /**
     * 导出优惠券套餐列表
     */
    @GetMapping("/export")
    public JsonResult export(CouponMeal couponMeal) {
        List<CouponMeal> list = couponMealService.selectCouponMealList(couponMeal);
        ExcelUtil<CouponMeal> util = new ExcelUtil<CouponMeal>(CouponMeal.class);
        return util.exportExcel(list, "meal");
    }

    /**
     * 获取优惠券套餐详细信息
     */
    @GetMapping(value = "/{mealId}")
    public JsonResult getInfo(@PathVariable("mealId") Integer mealId) {
        return JsonResult.ok(couponMealService.selectCouponMealById(mealId));
    }

    /**
     * 新增优惠券套餐
     */
    @PostMapping
    @OperateLog(module = ModuleEnum.COUPONPACKAGE, methods = MethodEnum.COUPONPACKAGE_ADD)
    public JsonResult add(@RequestBody CouponMeal couponMeal) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        if (StrUtil.isNotEmpty(couponMeal.getName()) && couponMeal.getName().length() > 100){
            throw new BaseException("优惠券套餐名称过长");
        }else if (StrUtil.isNotEmpty(couponMeal.getDiscount()) && couponMeal.getDiscount().length() > 200){
            throw new BaseException("优惠券套餐折扣描述过长");
        }
        return JsonResult.toAjax(couponMealService.insertCouponMeal(couponMeal), context);
    }

    /**
     * 修改优惠券套餐
     */
    @PutMapping
    @OperateLog(module = ModuleEnum.COUPONPACKAGE, methods = MethodEnum.COUPONPACKAGE_UPDATE)
    public JsonResult edit(@RequestBody CouponMeal couponMeal) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        return JsonResult.toAjax(couponMealService.updateCouponMeal(couponMeal), context);
    }

    /**
     * 删除优惠券套餐
     */
    @DeleteMapping("/{mealIds}")
    @OperateLog(module = ModuleEnum.COUPONPACKAGE, methods = MethodEnum.COUPONPACKAGE_DEL)
    public JsonResult remove(@PathVariable Integer[] mealIds) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        return JsonResult.toAjax(couponMealService.deleteCouponMealByIds(mealIds), context);
    }
}
